import { MigrationInterface, QueryRunner, Table, TableForeignKey } from "typeorm";

export class CreateDocumentChunk1700000000000 implements MigrationInterface {
    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.createTable(
            new Table({
                name: "document_chunk",
                columns: [
                    {
                        name: "id",
                        type: "int",
                        isPrimary: true,
                        isGenerated: true,
                        generationStrategy: "increment",
                    },
                    {
                        name: "content",
                        type: "text",
                        comment: "文档分块内容",
                    },
                    {
                        name: "embedding",
                        type: "json",
                        comment: "向量化数据",
                    },
                    {
                        name: "chunkIndex",
                        type: "int",
                        comment: "分块索引",
                    },
                    {
                        name: "chunkSize",
                        type: "int",
                        comment: "分块大小",
                    },
                    {
                        name: "metadata",
                        type: "json",
                        isNullable: true,
                        comment: "元数据",
                    },
                    {
                        name: "articleId",
                        type: "int",
                        comment: "关联的文章ID",
                    },
                    {
                        name: "createdAt",
                        type: "timestamp",
                        default: "CURRENT_TIMESTAMP",
                        comment: "创建时间",
                    },
                ],
            }),
            true
        );

        // 添加外键约束
        await queryRunner.createForeignKey(
            "document_chunk",
            new TableForeignKey({
                columnNames: ["articleId"],
                referencedColumnNames: ["id"],
                referencedTableName: "article",
                onDelete: "CASCADE",
            })
        );
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.dropTable("document_chunk");
    }
} 